{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using radial basis functions for smoothing/interpolation\n",
    "========================================================\n",
    "\n",
    "Radial basis functions can be used for smoothing/interpolating scattered\n",
    "data in n-dimensions, but should be used with caution for extrapolation\n",
    "outside of the observed data range.\n",
    "\n",
    "1d example\n",
    "----------\n",
    "\n",
    "This example compares the usage of the Rbf and UnivariateSpline classes\n",
    "from the scipy.interpolate module."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.interpolate import Rbf, InterpolatedUnivariateSpline\n",
    "\n",
    "import matplotlib\n",
    "matplotlib.use('Agg')\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# setup data\n",
    "x = np.linspace(0, 10, 9)\n",
    "y = np.sin(x)\n",
    "xi = np.linspace(0, 10, 101)\n",
    "\n",
    "# use fitpack2 method\n",
    "ius = InterpolatedUnivariateSpline(x, y)\n",
    "yi = ius(xi)\n",
    "\n",
    "plt.subplot(2, 1, 1)\n",
    "plt.plot(x, y, 'bo')\n",
    "plt.plot(xi, yi, 'g')\n",
    "plt.plot(xi, np.sin(xi), 'r')\n",
    "plt.title('Interpolation using univariate spline')\n",
    "\n",
    "# use RBF method\n",
    "rbf = Rbf(x, y)\n",
    "fi = rbf(xi)\n",
    "\n",
    "plt.subplot(2, 1, 2)\n",
    "plt.plot(x, y, 'bo')\n",
    "plt.plot(xi, fi, 'g')\n",
    "plt.plot(xi, np.sin(xi), 'r')\n",
    "plt.title('Interpolation using RBF - multiquadrics')\n",
    "plt.tight_layout()\n",
    "plt.savefig('rbf1d.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](files/attachments/RadialBasisFunctions/rbf1dnew.png)\n",
    "\n",
    "2d example\n",
    "==========\n",
    "\n",
    "This example shows how to interpolate scattered 2d data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.interpolate import Rbf\n",
    "\n",
    "import matplotlib\n",
    "matplotlib.use('Agg')\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import cm\n",
    "\n",
    "# 2-d tests - setup scattered data\n",
    "x = np.random.rand(100)*4.0-2.0\n",
    "y = np.random.rand(100)*4.0-2.0\n",
    "z = x*np.exp(-x**2-y**2)\n",
    "ti = np.linspace(-2.0, 2.0, 100)\n",
    "XI, YI = np.meshgrid(ti, ti)\n",
    "\n",
    "# use RBF\n",
    "rbf = Rbf(x, y, z, epsilon=2)\n",
    "ZI = rbf(XI, YI)\n",
    "\n",
    "# plot the result\n",
    "n = plt.normalize(-2., 2.)\n",
    "plt.subplot(1, 1, 1)\n",
    "plt.pcolor(XI, YI, ZI, cmap=cm.jet)\n",
    "plt.scatter(x, y, 100, z, cmap=cm.jet)\n",
    "plt.title('RBF interpolation - multiquadrics')\n",
    "plt.xlim(-2, 2)\n",
    "plt.ylim(-2, 2)\n",
    "plt.colorbar()\n",
    "plt.savefig('rbf2d.png')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
